//
//  ZNLVisitorView.swift
//  My_Weibo
//
//  Created by 正能量 on 16/8/14.
//  Copyright © 2016年 正能量. All rights reserved.
//

import UIKit
import SnapKit

/// MARK - 访客视图协议
protocol VisitorViewDelegate: NSObjectProtocol{
    
    func visitorViewDidRegister()
    
    func visitorViewDidLogin()
}

class ZNLVisitorView: UIView {
    
    /// MARK - 代理属性
   weak var delegate:VisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //加载控件
        setUpUI()
        ///添加点击事件
       registerBtn.addTarget(self, action: #selector(ZNLVisitorView.didClickRegisterBtn), forControlEvents: .TouchUpInside)
        
       loginBtn.addTarget(self, action: #selector(ZNLVisitorView.didClickLoginBtn), forControlEvents: .TouchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //用户信息
    func setUpInfo(imageName:String? = nil ,title: String?) {
        messageLbl.text = title
        
        // 如果图像为nil，表示是首页
        guard let imageName = imageName else {
            
            return
        }
        // 不是首页需要隐藏房子

        homeIcon.hidden = true
        coverView.hidden = true
        iconView.image = UIImage(named: imageName)
        
    }
    //动画
    private func setUpAnimation() {
        let animotion = CABasicAnimation(keyPath: "transform.rotation")
        animotion.toValue = 2 * M_PI
        animotion.repeatCount = MAXFLOAT
        animotion.duration = 20
        
        animotion.removedOnCompletion = false
        
        iconView.layer.addAnimation(animotion, forKey: nil)

    }
    
    ///MARK - 创建私有控件
    lazy var iconView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩
    lazy var coverView : UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //房子
    lazy var homeIcon : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //提示文字
    lazy var messageLbl : UILabel = UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", alignment: .Center)
    
    ///注册按钮
   private lazy var registerBtn : UIButton = UIButton(title: "注册", backgroundImage: "common_button_white_disable")
    
    
    ///登陆按钮
   private lazy var loginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    
        return btn
    }()
    
    /// 监听点击登陆方法
    @objc private func didClickLoginBtn (){
        print("didClickLoginBtn")
        delegate?.visitorViewDidLogin()
    }
    
    @objc private func didClickRegisterBtn() {
        print("didClickRegisterBtn")
        delegate?.visitorViewDidRegister()
    }
    
}

extension ZNLVisitorView {
    
    private func setUpUI() {
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeIcon)
        addSubview(messageLbl)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 设置文字对齐方式
        messageLbl.textAlignment = .Center
        
        //设置约束
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        
        coverView.snp_makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        homeIcon.snp_makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        messageLbl.snp_makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.centerX.equalTo(self)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.top.equalTo(messageLbl.snp_bottom).offset(20)
            make.left.equalTo(messageLbl.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.top.equalTo(registerBtn)
            make.left.equalTo(registerBtn.snp_right).offset(20)
            make.size.equalTo(registerBtn)
            
        }
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)

    }
}